package com.ztools.pc.service.impl.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.sys.Permission;
import com.ztools.common.model.pc.sys.PermissionTreeItem;
import com.ztools.common.util.common.PageUtil;
import com.ztools.pc.async.RedisAsync;
import com.ztools.pc.mapper.sys.PermissionMapper;
import com.ztools.pc.service.sys.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:一口香菜
 * @Date: 2023/3/4  15:34
 * @Version 1.0
 */
@Transactional
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    private RedisAsync redisAsync;

    @Override
    public List<Permission> findByIds(List<Long> ids) {
        return super.listByIds(ids);
    }


    @Override
    public Page<Permission> findList(PageUtil<Permission> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>(pageUtil.getParams());
        Page<Permission> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        return page(pages, queryWrapper);
    }

    @Override
    public List<PermissionTreeItem> findPermissionTreeData() {
        List<Permission> permissionList = list();
        List<PermissionTreeItem> permissionTreeItemList = new ArrayList<>();
        List<Integer> usedIndexList = new ArrayList<>();
        for (int i = 0; i < permissionList.size(); i++) {
            Permission p = permissionList.get(i);
            if ("/".equals(p.getPath())) {
                // 找到根节点
                usedIndexList.add(i);
                PermissionTreeItem root = new PermissionTreeItem();
                root.setId(p.getId());
                root.setParentId(p.getParentId());
                root.setName(p.getName());
                root.setRemark(p.getRemark());
                root.setPath(p.getPath());
                root.setChildren(getChildren(p.getId(), permissionList, usedIndexList));
                permissionTreeItemList.add(root);
                break;
            }
        }
        return permissionTreeItemList;
    }

    /**
     * 获取该菜单下的子菜单树结构数据
     *
     * @param parentId
     * @param permissionList
     * @param usedIndexList
     * @return
     */
    private List<PermissionTreeItem> getChildren(Long parentId, List<Permission> permissionList, List<Integer> usedIndexList) {
        List<PermissionTreeItem> children = new ArrayList<>();
        for (int i = 0; i < permissionList.size(); i++) {
            if (!usedIndexList.contains(i)) {
                Permission data = permissionList.get(i);
                if (parentId.equals(data.getParentId())) {
                    usedIndexList.add(i);
                    PermissionTreeItem node = new PermissionTreeItem();
                    node.setId(data.getId());
                    node.setName(data.getName());
                    node.setPath(data.getPath());
                    node.setRemark(data.getRemark());
                    node.setChildren(getChildren(data.getId(), permissionList, usedIndexList));
                    children.add(node);
                }
            }
        }
        return children;
    }

    @Override
    public boolean update(Permission permission) {
        boolean b = false;
        Permission permissionById = getById(permission);
        b = updateById(permission);
        if (b) {
            redisAsync.doAfterPermissionChange();
        }
        return b;
    }

    @Override
    public boolean delete(Permission permission) {
        boolean b = false;
        Permission permissionById = getById(permission);
        b = removeById(permission);
        if (b) {
            redisAsync.doAfterPermissionChange();
        }
        return b;
    }

    @Override
    public boolean add(Permission permission) {
        boolean b = false;
        b = save(permission);
        if (b) {
            redisAsync.doAfterPermissionChange();
        }
        return b;
    }

}
